/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-07-22 09:33:41
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-07-26 09:49:40
 * @FilePath: /hw/110/pthread/testpthead.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include <string>
#include <pthread.h>
#include <cstdio>
#include <unistd.h>
#include <vector>
#include<thread>

using namespace std;

// 模拟多线程运行

class Task
{
public:
    Task()
    {
    }
    ~Task()
    {
    }
    void Set(int x, int y)
    {
        _Datax = x;
        _Datay = y;
    }
    int Excute()
    {
        return _Datax + _Datay;
    }

private:
    int _Datax;
    int _Datay;
};

class ThreadData : public Task
{
public:
    ThreadData(int x, int y, const string &threadname) : _threadname(threadname)
    {
        _t.Set(x, y);
    }
    ~ThreadData()
    {
    }
    string threadname()
    {
        return _threadname;
    }
    int run()
    {
        return _t.Excute();
    }

private:
    string _threadname;
    Task _t;
};

class Result
{
public:
    Result()
    {
    }
    void SetResult(int result, string &threadname)
    {
        _result = result;
        _threadname = threadname;
    }
    ~Result()
    {
    }
    void Print()
    {
        cout << _threadname << " : " << _result << endl;
    }

private:
    int _result;
    string _threadname;
};
void *handerTask(void *args)
{
    ThreadData *td = static_cast<ThreadData *>(args);
    // 获取线程名字
    string threadname = td->threadname();
    Result *res = new Result();
    int result = td->run();

    res->SetResult(result, threadname);

    delete td;
    sleep(2);
    return res;
}
int main()
{

    // 创建多线程
    vector<pthread_t> threads;

    for (int i = 0; i < 5; i++)
    {
        char name[64];
        snprintf(name, 64, "Thread-%d", i + 1);
        ThreadData *td = new ThreadData(10, 20, name);

        pthread_t tid;
        pthread_create(&tid, nullptr, handerTask, td);
        threads.push_back(tid);
    }
    vector<Result *> result_set;
    void *ret = nullptr;

    for (auto &tid : threads)
    {
        // 等待线程
        pthread_join(tid, &ret);
        result_set.push_back((Result *)ret);
    }
    for (auto &res : result_set)
    {
        res->Print();
        delete res;
    }
}

// string ToHex(pthread_t tid)
// {
//     char id[64];
//     snprintf(id,sizeof(id),"0x%lx",tid);
//     return id;
// }
// void *hander(void*args)
// {
//     int cnt=5;
//     while(cnt)
//     {
//         cout<<" new thread running cnt "<<cnt<<" pid: "<<getpid()<<" mythreadid is "<<ToHex(pthread_self())<<endl;
//         sleep(1);
//          cnt--;
//     }

//     return nullptr;
// }
// int main()
// {
//     pthread_t tid;
//     //创建线程
//     pthread_create(&tid,nullptr,hander,nullptr);
//     //主线程
//     int cnt=10;
//     while(cnt)
//     {
//         cout<<" main thread running,cnt: "<<cnt<<" pid: "<<getpid()<<" new threadid is: "<<ToHex(tid)<<" main threadid is "<<ToHex(pthread_self())<<endl;
//         sleep(1);
//         cnt--;
//     }
//     return 0;
// }